﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quality.BasicData
{  
   
 
    public partial class productsedit : Form
    {
        public String id { get; set; }
        public productsedit(string id)
        {
            this.id = id;
            InitializeComponent();
            
            
        }
        private void productsedit_Load(object sender, EventArgs e)
        {
            DB.getcombobox(customer, "customers", "num", "cusname");

            DB.getcombobox(supplier, "suppliers", "num", "supname");

            DB.getcombobox(testtype, "testtypes", "num", "typename");

            if (id == "")
            {
                num.Text ="";
                proname.Text = "";
                specification.Text = "";
                unit1.Text = "";

                customer.SelectedIndex = -1;
                supplier.SelectedIndex = -1;
                testtype.SelectedIndex = -1;

            }
            else {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand();

                da.SelectCommand.CommandText = "select * from products where id=@id";
                da.SelectCommand.Parameters.AddWithValue("@id", id);
                DataSet dt = DB.getlist(da);
                DataRow row = dt.Tables[0].Rows[0];
                num.Text = row["num"].ToString();
                proname.Text = row["proname"].ToString();
                specification.Text = row["specification"].ToString();
                unit1.Text = row["unit1"].ToString();

                customer.SelectedValue = row["customer"].ToString();
                supplier.SelectedValue= row["supplier"].ToString();
                testtype.SelectedValue= row["testtype"].ToString();
                stopcheckBox.Checked=Convert.ToBoolean(row["stop"]);
            
            }
           

        }
        private void button1_Click(object sender, EventArgs e)
        {
           yyproducts frm = new yyproducts();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent(string[] value)
        {
            num.Text = value[0];
            proname.Text = value[1];
            specification.Text = value[2];
            unit1.Text = value[3];


        }
        public event TransfDelegate1 TransfEvent;
        private void savebutton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(num.Text)) { MessageBox.Show("编号不能为空！"); return; }
            if (string.IsNullOrEmpty(proname.Text)) { MessageBox.Show("成品名称不能为空！"); return; }
            if (string.IsNullOrEmpty(num.Text)) { MessageBox.Show("编号不能为空！"); return; }
            if (testtype.SelectedIndex==-1) { MessageBox.Show("检验类型不能为空！"); return; }
           

            SqlCommand comm = new SqlCommand();
            if (id == "")
            {
                int count = sqlHelper.Count("select * from products where num='" + num.Text + "'");
                if (count > 0) { MessageBox.Show("编号重复，不能保存！"); return; }
                comm.CommandText = "insert into products(num,proname,specification,unit1,customer,supplier,testtype,stop)values(@num,@proname,@specification,@unit1,@customer,@supplier,@testtype,@stop)";
            }
            else {

                comm.CommandText = "update products set num=@num,proname=@proname,specification=@specification,unit1=@unit1,customer=@customer,supplier=@supplier,testtype=@testtype,stop=@stop where id=@id";
            
            
            }
         

            //传值 username，不指定参数长度
            //查询计划为(@UserName varchar(8))select * from Users where UserName=@UserName
           // comm.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar) { Value = "username" });
            comm.Parameters.AddWithValue("@id",id);
            comm.Parameters.AddWithValue("@num", num.Text);
            comm.Parameters.AddWithValue("@proname", proname.Text);
            comm.Parameters.AddWithValue("@specification", specification.Text);
            comm.Parameters.AddWithValue("@unit1", unit1.Text);
            comm.Parameters.AddWithValue("@customer", (customer.SelectedIndex==-1)?"NULL":customer.SelectedValue);
            comm.Parameters.AddWithValue("@supplier", (supplier.SelectedIndex==-1)?"NULL":supplier.SelectedValue);
            comm.Parameters.AddWithValue("@testtype", (testtype.SelectedIndex==-1)?"NULL":testtype.SelectedValue);
            comm.Parameters.AddWithValue("@stop", stopcheckBox.Checked);
            DB.dosql(comm);
            TransfEvent();
            MessageBox.Show("保存成功！");
            this.Close();

        }

        
    }
}
